#include <actionlib/server/simple_action_server.h>
#include <ros/ros.h>
#include <ros1_protobuf_msg_bridge/action/ActDemoAction.h>

class ActDemoAction1
{
protected:
    ros::NodeHandle nh_;
    actionlib::SimpleActionServer<ros1_protobuf_msg_bridge::ActDemoAction> as_;
    std::string action_name_;

    ros1_protobuf_msg_bridge::ActDemoFeedback feedback_;
    ros1_protobuf_msg_bridge::ActDemoResult result_;

public:
    ActDemoAction1(std::string name) : as_(nh_, name, boost::bind(&ActDemoAction1::executeCB, this, _1), false), action_name_(name) {
        as_.start();
    }

    ~ActDemoAction1(void) {
    }

    void executeCB(const ros1_protobuf_msg_bridge::ActDemoGoalConstPtr &goal) {
        ROS_INFO("act: %d,%s", (int)goal.get()->val(), goal.get()->key().c_str());
        ros::Rate r(1);
        bool success = true;

        for (int i = 1; i <= 10; i++) {
            if (as_.isPreemptRequested() || !ros::ok()) {
                as_.setPreempted();
                success = false;
                break;
            }
            feedback_.set_val(i);
            as_.publishFeedback(feedback_);
            ROS_INFO("feedback,val= %d", i);
            r.sleep();
        }

        if (success) {
            result_.set_val(22333);
            as_.setSucceeded(result_);
        }
    }
};

int main(int argc, char **argv) {
    ros::init(argc, argv, "act_server");
    ActDemoAction1 do_act1("t_act1");
    ros::spin();
    return 0;
}
